#include "helper.h"
#include <tools.h>
#include <iostream>
#include <matrix44.h>
#include <vect.h>
#include <GLTools.h>
#include <GLMatrixStack.h>
#include <GLGeometryTransform.h>
#include <math3d.h>
#include <freeglut.h>
using namespace std;

void GetPositionsFromShader()
{
	// lights
	EngineTools::GetUniformPositionFromShader(shaderPositionLocation, shader, "pointLight.positionInEyeSpace", true);
	EngineTools::GetUniformPositionFromShader(shaderColorLocation, shader, "pointLight.color", true);
	//EngineTools::GetUniformPositionFromShader(shaderAngleLocation, shader, "pointLight.angle", true);
	EngineTools::GetUniformPositionFromShader(shaderAttenuationLocation, shader, "pointLight.attenuation", true);

	// material properties
	EngineTools::GetUniformPositionFromShader(shaderAmbientColorLocation, shader, "materialProperties.ambientColor", true);
	EngineTools::GetUniformPositionFromShader(shaderDiffuseColorLocation, shader, "materialProperties.diffuseColor", true);
	EngineTools::GetUniformPositionFromShader(shaderSpecularColorLocation, shader, "materialProperties.specularColor", true);
	EngineTools::GetUniformPositionFromShader(shaderSpecularExponentLocation, shader, "materialProperties.specularExponent", true);

	// ambient light
	EngineTools::GetUniformPositionFromShader(shaderAmbientLightLocation, shader, "ambientLight", true);
}

void SendDataToGPU(Matrix44<float>& lightMatrix)
{
	// at first we musst properly compute position vector for light computations
	//Vect<float> pos(position[0], position[1], position[2]);
	//Vect<float> lightPositionInEyeSpace = lightMatrix * pos;
	//lightPositionInEyeSpace = -lightPositionInEyeSpace;
	//lightPositionInEyeSpace.normalize();

	M3DMatrix44f mdlv; 
	glGetFloatv(GL_MODELVIEW_MATRIX, mdlv);
	M3DVector3f lightPositionInEyeSpace;
	m3dTransformVector3(lightPositionInEyeSpace, position, mdlv);

	// lights
	//glUniform3fv(shaderPositionLocation, 1, lightPositionInEyeSpace.getArrayPointer());
	glUniform3fv(shaderPositionLocation, 1, lightPositionInEyeSpace);
	glUniform3fv(shaderColorLocation, 1, color);
	//glUniform1f(shaderAngleLocation, angle);
	glUniform3fv(shaderAttenuationLocation, 1, attenuation);

	// material properties
	glUniform3fv(shaderAmbientColorLocation, 1, ambientColor);
	glUniform3fv(shaderDiffuseColorLocation, 1, diffuseColor);
	glUniform3fv(shaderSpecularColorLocation, 1, specularColor);
	glUniform1f(shaderSpecularExponentLocation, (GLfloat)exponent);

	// ambient light
	glUniform3fv(shaderAmbientLightLocation, 1, ambientLight);
}

void DebugUniforms()
{
	cout << "shaderPositionLocation = " << EngineTools::DebugUniform( shader, shaderPositionLocation, 3 );
}

void DrawLightPosition()
{
	glPushMatrix();
	glTranslatef(position[0], position[1], position[2]);
		glutWireCube(0.2);
	glPopMatrix();
}